<?php
/**
 * 监理报告管理
 * @author yangL
 **/
namespace Admin\Controller;
use Common\Controller\WahahabaseController;
class SupervisionReportController extends WahahabaseController{
	
	public function _initialize() {
		parent::_initialize();
		$this->deal_model = D("Common/SupervisionReport");
		$this->att_model = D("Common/Attachment");
        $this->department_model = D("Common/Department");
        $this->consignee_model = D("Common/Consignee");		
		$this->check = array('code');
	}
    public function indexPage($where) {
        $eq = array(
            "eq" => "=",
            "neq" => "!=",
            "gt" => ">",
            "egt" => ">=",
            "lt" => "<",
            "elt" => "<=",
            "like" => "LIKE"
        );
        // 当前登录人
        $adminId = session('ADMIN_ID');

        $condition = "d.is_deleted <> 1 ";

        $condition .= session('deptid') == 0 ? " AND (1 = 1 " : " AND ((SELECT department_role FROM hzzy_department WHERE id='" . session("deptid") . "') = 3 ";

        $condition .= "OR (('" . session("deptid") . "' IN (SELECT department_id FROM hzzy_consignee WHERE table_id = d.id AND belong_to='1')) AND d.is_publish = '1'))";
        
        $condition .= "OR (d.is_publish = 1 AND ((SELECT IFNULL(department_role,0) as `level` FROM hzzy_users u1 LEFT JOIN hzzy_department d1 ON u1.department_id = d1.id WHERE u1.id=$adminId) <= (SELECT IFNULL(department_role,0) as `level` FROM hzzy_users u2 LEFT JOIN hzzy_department d2 ON u2.department_id = d2.id WHERE u2.id=d.add_user)))"; 
        foreach ($where as $key => $v) {
            if (count($v) > 1) {
                $d = $eq[$v[0]] == NULL ? '=' : $eq[$v[0]];
                if ($d != 'LIKE') {
                    $condition .= " AND $key $d '" . $v[1] . "'";
                } else {
                    $condition .= " AND $key $d '%" . $v[1] . "%'";
                }
            } else {
                $condition .= " AND $key = '$v'";
            }
        }
        $count = $this->deal_model->alias("d")->where($condition)->count();
        $page = $this->page($count, 20);

        $results = $this->deal_model->alias("d")
                ->field('d.*,u.real_name')
                ->join(array(
                    ' LEFT JOIN ' . C('DB_PREFIX') . 'users u ON d.add_user = u.id',
                ))
                ->where($condition)
                ->order("d.add_time DESC")
                ->limit($page->firstRow, $page->listRows)
                ->select();
        //exit($this->deal_model->getLastSql());    
        $this->assign("page", $page->show('Admin'));
        $this->assign("results", $results);

        $this->display();
    }
	/**
	 *  主页
	 */
	public function index(){
		$where = array("d.is_deleted"=>array('neq', 1));
		$sub_project = trim(I('request.sub_project'));
		if($sub_project){
			$where['sub_project'] = array('like',"%$sub_project%");
		}
        $this->getDepartLevel();
		$this->indexPage($where);
	}
	
	/**
	 *  添加
	 */
	public function add(){
        $Department = $this->department_model->where(array('is_deleted' => 0,'department_role' => 5))->field("id,department_name")->order("id DESC")->select();
        $this->assign("data", $Department);
		$this->display();
	}
	    public function add_post() {
        if (IS_POST) {
            $data = array_merge($_POST, $this->saveData());
            if ($this->deal_model->create() !== false) {
                $result = $this->deal_model->add($data);
                if ($result !== false) {
                    $this->saveFile($result, 6);
                    $cons = array(
                        "belong_to" => '1',
                        "table_id" => $result
                    );
                    foreach ($data['carbon_copy_department'] as $key => $vo) {
                        $cons['consignee_type'] = '2';
                        $cons['department_id'] = $vo;
                        $this->consignee_model->add($cons);
                    };
                    $this->success("添加成功！", U("SupervisionReport/index"));
                } else {
                    $this->error("添加失败！");
                }
            } else {
                $this->error($this->deal_model->getError());
            }
        }
    }
	
	/**
	*  修改
	*/
    public function edit() {
        $this->getInfo();
    }
    public function edit_post() {
        if (IS_POST) {
            $data = array_merge($_POST, $this->saveData(2));
            if ($this->deal_model->create() !== false) {
                $result = $this->deal_model->save($data);
                if ($result !== false) {
					$this->saveFile(I("post.id"), 6, 2);
                    $this->consignee_model->where(array('belong_to' => 1, 'table_id' => I("post.id", 0)))->delete();
                    $cons = array(
                        "belong_to" => '1',
                        "table_id" => I("post.id", 0)
                    );
                    foreach ($data['carbon_copy_department'] as $key => $vo) {
                        $cons['consignee_type'] = '2';
                        $cons['department_id'] = $vo;
                        $this->consignee_model->add($cons);
                    };
                    $this->success("保存成功！", U("SupervisionReport/index"));
                } else {
                    $this->error("保存失败！");
                }
            } else {
                $this->error($this->deal_model->getError());
            }
        }
    }
	
	/**
	 * 查看
	 **/
    public function view() {
        $this->getDepartLevel();
        $this->getInfo();
    }
    private function getInfo() {
        $id = I('get.id', 0, 'intval');
        $work = $this->deal_model->where(array("id" => $id))->find();
        $this->assign("result", $work);

		$results = $this->att_model->where(array("table_id"=>$id, "belong_to"=>6))->select();
		$this->assign("results", $results);

        $Department = $this->department_model->field("id,department_name")->where(array("is_deleted" => array('neq', 1),'department_role' => 5))->select();
        $this->assign("data", $Department);

        $Department = $this->consignee_model->field("department_id")->where(array('consignee_type' => 1, 'belong_to' => 1, 'table_id' => $id))->select();
        $result = array();
        foreach ($Department as $key => $vo) {
            $result[] = $vo['department_id'];
        }
        $this->assign("dept1", json_encode($result));

        $Department = $this->consignee_model->field("department_id")->where(array('consignee_type' => 2, 'belong_to' => 1, 'table_id' => $id))->select();
        $result = array();
        foreach ($Department as $key => $vo) {
            $result[] = $vo['department_id'];
        }
        $this->assign("dept2", json_encode($result));

        $this->display();
    }
}


?>